Page({
  data: {
    wxSearchData: {
      value: '',
      tipKeys: [],
      his: [],
      hotKeys: [],
      view: {}
    }
  },

  init: function () {
    let _that = this
    let view = {}
    wx.getSystemInfo({
      success: function (res) {
        let proportion = 750/res.windowWidth;
        let barHeight = Math.floor(102/proportion)
        view.barHeight = barHeight;
        view.seachHeight = res.windowHeight - barHeight;
        _that.setData({
          'wxSearchData.view': view
        });
      }
    });
    this.getHistories()
    this.getHot()
  },

  //获取热点
  getHot: function () {
    this.setData({
      'wxSearchData.hotKeys': ['饭卡', '一教', '吊坠']
    })
  },

  //获取搜索记录
  getHistories: function () {
    try {
      let value = wx.getStorageSync('histories');
      if (value.length > 0) {
        this.setData({
          'wxSearchData.his': value
        })
      }
    } catch (e) {
      console.log(e)
    }
  },

  //储存搜索记录
  setHistories: function (histories) {
    console.log(histories)
    wx.setStorage({
      key: "histories",
      data: histories
    })
  },

  //清除搜索记录
  removeHistories: function () {
    let _this = this
    wx.removeStorage({
      key: 'histories',
      success: function () {
        _this.setData({
          'wxSearchData.his': []
        });
      }
    })
  },

  // 输入变化时的操作
  wxSearchInput: function (e){
    let _this = this
    _this.setData({
      'wxSearchData.value': e.detail.value
    })
    if(!e.detail.value){
      _this.setData({
        'wxSearchData.tipKeys': []
      })
      return
    }
    wx.cloud.callFunction({
      name: "search",
      data: {
        $url: 'getTip',
        keyword: e.detail.value
      }
    }).then(res => {
      if (res.result.code === 200) {
        let data = [];
        for (let item of res.result.data) {
          for (let kw of item.keywords) {
            if (kw.indexOf(e.detail.value) != -1 && data.indexOf(kw) == -1) {
              data.push(kw)
            }
          }
        }
        _this.setData({ //设置结果集
          'wxSearchData.tipKeys': data
        })
      }
    })
  }, 

  // 点击提示或者关键字、历史记录时的操作
  wxSearchKeyTap: function (e){
    this.search(e.target.dataset.key);
  },

  // 删除所有的历史记录
  wxSearchDeleteAll: function(){
    this.removeHistories()
  },

  // 确认搜索
  wxSearchConfirm: function(e){
    var key = e.target.dataset.key;
    if(key=='back'){
      this.goback();
    }else{
      this.search(this.data.wxSearchData.value);
    }
  },

  // 清空函数
  wxSearchClear: function () {
    let temData = this.data.wxSearchData;
    // 更新数据
    temData.value = '';
    temData.tipKeys = [];
    // 更新视图
    this.setData({
      wxSearchData: temData
    });
  },

  // 搜索回调函数  
  search: function (value) {
    if (!value) {
      wx.showToast({
        title: '搜索不能为空',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '请稍等',
    })
    let histories = this.data.wxSearchData.his;
    let findPostion = histories.indexOf(value);
    if (findPostion !== -1) {
      histories.splice(findPostion, 1)
      histories.unshift(value)
    } else {
      histories.unshift(value)
    }
    this.setHistories(histories)
    //云端记录
    wx.cloud.callFunction({
      name: "search",
      data: {
        $url: 'recode',
        value
      }
    })
    //前往搜索
    wx.hideLoading()
    this.goback(value)
  },

  // 返回回调函数
  goback: function (value) {
    const pages = getCurrentPages();//获取页面栈
    const prevPage = pages[pages.length - 2];  //前一个页面
    wx.navigateBack({
      delta:1,
      success: function () {
        if(value){
          prevPage.setData({
            result: [],
            searchValue: value
          });
          prevPage._loadData(); 
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
  },

  /**
   * 生命周期函数--监听页面渲染
   */
  onShow: function (options) {

  },

});